package com.walkline.screen;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class RegisterScreen extends PopupScreen
{
	private LabelField _labelTitle;
	private LabelField _labelNotice;
	private EditField _blockField;
	private ButtonField _buttonOk;

	public RegisterScreen()
	{
		super(new VerticalFieldManager());

		_labelTitle = new LabelField("注册", Field.FIELD_HCENTER);
		_labelTitle.setMargin(0,  0, 5, 0);
		_blockField=new EditField("输入注册码：", null, 6, EditField.NO_NEWLINE);
		_labelNotice = new LabelField("\u2022 购买请在淘宝搜索IWeather（这是爱天气的购买链接，暂时用来购买爱调频，拍下后请备注爱调频）。购买后请旺旺联系，或者通过邮件与微博联系 @折腾笔记", LabelField.NON_FOCUSABLE);
		_labelNotice.setFont(Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) - 2, Ui.UNITS_pt));
		_buttonOk = new ButtonField("确定", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
		_buttonOk.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context) {close();}
		});
		_buttonOk.setMinimalWidth(100);

		ButtonField buttonCancel = new ButtonField("取消", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
		buttonCancel.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context) 
			{
				_blockField.setText("");
				onClose();
			}
		});
		buttonCancel.setMinimalWidth(100);

		add(_labelTitle);
		add(_blockField);
		add(_labelNotice);
		HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.FIELD_HCENTER);
		hfm.add(_buttonOk);
		hfm.add(buttonCancel);
		add(hfm);
	}

	public String getRegisterCode() {return _blockField.getText();}

	public boolean onClose()
	{
		UiApplication.getUiApplication().popScreen(this);
		return true;
	}

	protected boolean onSavePrompt() {return true;}
	
	protected boolean keyChar(char character, int status, int time)
	{
		switch (character)
		{
			case Characters.ESCAPE:
				_blockField.setText("");
				onClose();
				return true;
			case Characters.ENTER:
				if (_blockField.isFocus())
				{
					_buttonOk.setFocus();
					return true;
				} 				
		}

		return super.keyChar(character, status, time);
	}
}